package com.ddm.controller.wxadmin;

import com.ddm.result.Result;
import com.ddm.service.vxService.ContestRankService;
import com.ddm.vo.vxVo.ContestRankVO;
import com.ddm.vo.vxVo.ContestRankStatisticsVO;
import com.ddm.utils.UserNameMaskUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/contest-rank")
@Api(tags = "管理端竞赛排行榜相关接口")
@Slf4j
public class AdminContestRankController {

    @Autowired
    private ContestRankService contestRankService;

    @GetMapping("/{examId}")
    @ApiOperation("获取竞赛排行榜（管理端）")
    public Result<List<ContestRankVO>> getContestRanking(
            @ApiParam(value = "竞赛ID", required = true) @PathVariable Long examId,
            @ApiParam(value = "限制返回数量，默认100") @RequestParam(defaultValue = "100") Integer limit) {
        log.info("获取竞赛排行榜（管理端），竞赛ID: {}, 限制数量: {}", examId, limit);
        List<ContestRankVO> ranking = contestRankService.getContestRanking(examId, limit);
        
        // 管理端显示部分脱敏的用户名
        ranking.forEach(rank -> {
            if (rank.getOriginalUserName() != null) {
                rank.setUserName(UserNameMaskUtil.getPartialMaskedUserName(rank.getOriginalUserName()));
            }
        });
        
        return Result.success(ranking);
    }

    @GetMapping("/{examId}/statistics")
    @ApiOperation("获取竞赛排行榜统计信息（管理端）")
    public Result<ContestRankStatisticsVO> getContestRankStatistics(
            @ApiParam(value = "竞赛ID", required = true) @PathVariable Long examId) {
        log.info("获取竞赛排行榜统计信息（管理端），竞赛ID: {}", examId);
        return Result.success(contestRankService.getContestRankStatistics(examId));
    }

    @GetMapping("/{examId}/user/{userId}/rank")
    @ApiOperation("获取指定用户的竞赛排名（管理端）")
    public Result<ContestRankVO> getUserContestRank(
            @ApiParam(value = "竞赛ID", required = true) @PathVariable Long examId,
            @ApiParam(value = "用户ID", required = true) @PathVariable Long userId) {
        log.info("获取指定用户的竞赛排名（管理端），竞赛ID: {}, 用户ID: {}", examId, userId);
        return Result.success(contestRankService.getMyContestRank(examId, userId));
    }

    @GetMapping("/overall")
    @ApiOperation("获取综合竞赛排行榜（管理端）")
    public Result<List<ContestRankVO>> getOverallContestRanking(
            @ApiParam(value = "限制返回数量，默认50") @RequestParam(defaultValue = "50") Integer limit) {
        log.info("获取综合竞赛排行榜（管理端），限制数量: {}", limit);
        return Result.success(contestRankService.getOverallContestRanking(limit));
    }
} 